const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainNote: '',
    trainId:'',
    textNumber: 0,
    textList:[
      '今天训练感觉非常好。',
      '练得很到位，肌肉感觉明显。',
      '有些感受不到肌肉的发力感。',
      '比较轻松，可以加大强度。',
      '有点吃力，下次希望能轻松一点。'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainNote = wx.getStorageSync("trainNote");
    const trainId = wx.getStorageSync("trainId");
    this.setData({
      trainNote,
      trainId
    });
  },
  bindTextArea: function(e) {
    const trainNote = e.detail.value;
    this.setData({
      trainNote: trainNote,
      textNumber: trainNote.length
    });
  },
  toSave: function() {
    const openId = app.globalData.openId;
    const trainNote = this.data.trainNote;
    const trainId = this.data.trainId;
    const url = app.globalData.url;
    wx.request({
      url: url + '/train/saveNote',
      data: {
        trainId,
        trainNote
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          });
        }else{
          wx.showToast({
            title: '保存失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
    
  },
  addToNote: function(e) {
    const value = e.target.dataset.name;
    const trainNote = this.data.trainNote || '';
    this.setData({
      trainNote: trainNote + value,
      textNumber: trainNote.length
    });
  }
})